package Panel;
/**
 * Created by win10 on 2017/6/12.
 */

import java.awt.AWTException;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainPanel extends JPanel {

    //下侧右侧面板画图时的参数
    private int startX,startY,endX,endY;

    public MainPanel(){
        this.setPreferredSize(new Dimension(400,400));
        this.setLayout(new FlowLayout(FlowLayout.RIGHT));
    }



    public void showGui(){

        this.addMouseListener(new MouseAdapter(){
            //鼠标按下事件，重置startX，startY
            public void mousePressed(MouseEvent e){
                startX = e.getX();
                startY = e.getY();
            }

            //双击面板是保存绘图区
            public void mouseClicked(MouseEvent e){
                if(e.getClickCount() == 2){
                    // savePic(path+"/pic.jpg");
                }
            }
        });

        //鼠标拖动事件，自由画图
        this.addMouseListener(new MouseAdapter(){
            public void mouseDragged(MouseEvent e){
                Graphics graphics = getGraphics();
                endX = e.getX();
                endY = e.getY();
                ((Graphics2D) graphics).setColor(new Color(144,242,41));
                ((Graphics2D) graphics).setStroke(new BasicStroke(10));
                ((Graphics2D)graphics).drawLine(startX,startY,endX,endY);
                startX=endX;
                startY=endY;
            }
        });

    }

    /**
     * Created by Haskell_Curry on 2017/6/12.
     * 画圆测试
     */
    public void circle_Test(){
    }


    // public void savePic(String path){
    // 	BufferedImage myImage = null;
    // 	try {
    // 		myImage = new Robot().createScreenCapture(
    // 				new Rectangle(jFrame.getX()+8, jFrame.getY()+30, this.getWidth(), this.getHeight()));
    // 		ImageIO.write(myImage, "jpg", new File(path));
    // 	} catch (AWTException e) {
    // 		e.printStackTrace();
    // 	} catch (IOException e) {
    // 		e.printStackTrace();
    // 	}
    // }

    public static void main(String[] args) {
        MainPanel drawAndSave = new MainPanel();
        drawAndSave.showGui();
    }

}

